/**
 * Alipay.com Inc.
 * Copyright (c) 2009-2010 All Rights Reserved.
 */
package com.alipay.test.dbtest.script;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ScriptLoaderToExecuterAdapter
 * 
 * @author quickpoint
 * @version $Id: ScriptLoaderToExecuterAdapter.java, v 0.1 2010-5-29 下午08:46:41 quickpoint Exp $
 */
public class ScriptLoaderToExecuterAdapter implements ScriptLoader, ScriptExecuter {

    /**
     * logger
     */
    private static final Logger logger           = LoggerFactory
                                                     .getLogger(ScriptLoaderToExecuterAdapter.class);
    public static final String  COMMENT_TAG_STAR = "#";
    public static final String  COMMENT_TAG_LINE = "--";

    private ScriptExecuter      scriptExecuter   = null;

    /**
     * load and execute the script
     * @param script script file name
     * @throws ScriptException exception throws when failed to load and execute
     */
    public void loadAndExecuter(String script) throws ScriptException {
        load(script);
    }

    /* (non-Javadoc)
     * @see com.alipay.test.dbtest.script.ScriptLoader#load(java.lang.String)
     */
    public void load(String script) throws ScriptException {
        Validate.notNull(script, "script file name should not be null");

        load(this.getClass().getClassLoader().getResourceAsStream(script));
    }

    /* (non-Javadoc)
     * @see com.alipay.test.dbtest.script.ScriptLoader#load(java.io.InputStream)
     */
    public void load(InputStream script) throws ScriptException {
        Validate.notNull(script, "script stream should not be null");

        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(script));
            String line = null;
            while (null != (line = br.readLine())) {
                if (StringUtils.isNotBlank(line)) {
                    if (isCommentedLine(line)) {
                        continue;
                    }

                    // doExecute
                    execute(line);
                }
            }
        } catch (IOException ex) {
            throw new ScriptException(ex);
        } finally {
            closeSilently(br);
        }
    }

    private void closeSilently(BufferedReader br) {
        if (null != br) {
            try {
                br.close();
            } catch (IOException omit) {
                logger.error("", omit);
            } finally {
                br = null;
            }
        }
    }

    private boolean isCommentedLine(String line) {
        return StringUtils.startsWith(line, COMMENT_TAG_STAR)
               || StringUtils.startsWith(line, COMMENT_TAG_LINE);
    }

    /* (non-Javadoc)
     * @see com.alipay.test.dbtest.script.ScriptExecuter#execute(java.lang.String)
     */
    public void execute(String line) throws ScriptException {
        if (null != scriptExecuter) {
            scriptExecuter.execute(line);
        }
    }

    /**
     * @param scriptExecuter The scriptExecuter to set.
     */
    public void setScriptExecuter(ScriptExecuter scriptExecuter) {
        this.scriptExecuter = scriptExecuter;
    }

}
